/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.dac.method;


import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.api.repository.GspDbType;
import com.inspur.edp.cef.api.repository.adaptor.IEntityAdaptor;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.cef.generator.repository.dac.DataTypeDacGenContext;
import com.inspur.edp.cef.generator.repository.dac.DataTypeDacGenerator;
import com.inspur.edp.cef.generator.repository.dac.EntityDacGenContext;
import lombok.var;
import org.eclipse.jdt.core.dom.*;

import java.util.Map;

public class GetEntityAdaptorGenerator extends GetAdaptorGenerator {
	public GetEntityAdaptorGenerator(String fieldName, DataTypeDacGenContext parentContext) {
		super(fieldName, parentContext);
	}

	public GetEntityAdaptorGenerator(String fieldName, DataTypeDacGenContext parentContext, String nodeCode) {
		super(fieldName, parentContext, nodeCode);
	}

	@Override
	protected TypeInfo getReturnType() {
		return new TypeInfo(IEntityAdaptor.class.getTypeName());
	}

	//if (adaptor == null)
	//{
	//    var db = DatabaseUtil.getDatabase();
	//    switch (db.DbType)
	//    {
	//        case GSPDbType.PgSQL:
	//            adaptor = new SalesOrderAdaptor(db);
	//        case GSPDbType.Oracle:
	//            adaptor = new SalesOrderAdaptor(db);
	//        default:
	//            throw new Exception("暂不支持数据库类型" + db.DbType);
	//    }
	//}

	/*
	if(adapter==null)
		{
			switch (getDbType()) {
				case PgSQL:
					adapter = new PgSql();
					break;
				case DM:
					break;
				default:
					throw new IllegalStateException("Unexpected value: " + dbType);
			}
		}
	* */
	@Override
	protected IfStatement buildInitStatement() {
		IfStatement ifStatement=ast.newIfStatement();
		setIfStatementExpression(ifStatement);
		setIfStatementThen(ifStatement);
		return ifStatement;
	}

	private void setIfStatementExpression(IfStatement ifStatement) {
		InfixExpression infixExpression=ast.newInfixExpression();
		infixExpression.setLeftOperand(ast.newSimpleName(DataTypeDacGenerator.AdaptorFieldName));
		infixExpression.setOperator(InfixExpression.Operator.EQUALS);
		infixExpression.setRightOperand(ast.newNullLiteral());
		ifStatement.setExpression(infixExpression);
	}

	private void setIfStatementThen(IfStatement ifStatement) {
		Block block=ast.newBlock();
		Assignment assignment = ast.newAssignment();
		assignment.setLeftHandSide(ast.newSimpleName(DataTypeDacGenerator.AdaptorFieldName));
		assignment.setOperator(Assignment.Operator.ASSIGN);
		for (Map.Entry<String, String> adaptorType : ((EntityDacGenContext) dataTypeDacGenContext).getGeneratedAdaptorTypes().entrySet())
		{
			if(adaptorType.getKey().equals("base")==false)
				continue;
			ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
			classInstanceCreation.setType(ast.newSimpleType(ast.newName(adaptorType.getValue())));
			classInstanceCreation.arguments().add(ast.newBooleanLiteral(true));
			classInstanceCreation.arguments().add(ast.newThisExpression());
			assignment.setRightHandSide(classInstanceCreation);
			break;
		}
		ExpressionStatement expressionStatement = ast.newExpressionStatement(assignment);
		block.statements().add(expressionStatement);
		ifStatement.setThenStatement(block);
	}

	private void setSwitchStatementExpression(SwitchStatement switchStatement) {
		MethodInvocation methodInvocation=ast.newMethodInvocation();
		methodInvocation.setName(ast.newSimpleName("getDbType"));
		switchStatement.setExpression(methodInvocation);
	}

	private void setSwitchCases(SwitchStatement switchStatement) {
		setSwitchCase(GspDbType.PgSQL,switchStatement);
//		setSwitchCase(GspDbType.DM,switchStatement);
//		setSwitchCase(GspDbType.SQLServer,switchStatement);
//		setSwitchCase(GspDbType.Oracle,switchStatement);
//		setSwitchCase(GspDbType.HighGo,switchStatement);
	}

	private void setSwitchCase(GspDbType dbType,SwitchStatement switchStatement) {
		SwitchCase switchCase=ast.newSwitchCase();
		switchCase.setExpression(ast.newSimpleName(dbType.toString()));
		switchStatement.statements().add(switchCase);

		Assignment assignment=ast.newAssignment();
		assignment.setLeftHandSide(ast.newSimpleName(DataTypeDacGenerator.AdaptorFieldName));
		assignment.setOperator(Assignment.Operator.ASSIGN);

		for (Map.Entry<String, String> adaptorType : ((EntityDacGenContext) dataTypeDacGenContext).getGeneratedAdaptorTypes().entrySet())
		{
			if(adaptorType.getKey().equals(dbType.toString())==false)
			continue;
			ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
			classInstanceCreation.setType(ast.newSimpleType(ast.newName(adaptorType.getValue())));
			if(adaptorType.getKey().equals("PgSQL")){
				classInstanceCreation.arguments().add(ast.newSimpleName("adaptorItemList"));
				classInstanceCreation.arguments().add(ast.newThisExpression());
			}


			assignment.setRightHandSide(classInstanceCreation);
		}

		switchStatement.statements().add(ast.newExpressionStatement(assignment));
		BreakStatement breakStatement=ast.newBreakStatement();
		switchStatement.statements().add(breakStatement);
	}


	private void setSwitchDefaultCase(SwitchStatement switchStatement)
	{
		SwitchCase defaultCase =ast.newSwitchCase();
		defaultCase.setExpression(null);
		switchStatement.statements().add(defaultCase);
		addImport(CefException.class.getTypeName());
		ThrowStatement throwStatement=GeneratorUtil.getThrowStatementWithMessage(ast,"暂不支持数据库类型：");

//		ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
//		classInstanceCreation.setType(ast.newSimpleType(ast.newName(RuntimeException.class.getName())));
//		classInstanceCreation.arguments().add(GeneratorUtil.getStringLiteral(ast,"暂不支持数据库类型："));
//		InfixExpression infixExpression=ast.newInfixExpression();
//		infixExpression.setLeftOperand(GeneratorUtil.getStringLiteral(ast,"暂不支持数据库类型："));
//		infixExpression.setOperator(InfixExpression.Operator.PLUS);
//		infixExpression.setRightOperand();

		//throwStatement.setExpression(classInstanceCreation);
		switchStatement.statements().add(throwStatement);
	}
}
